home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / listpair.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.6 KB  |  85 lines  |  [TEXT/R*ch]

  1. (* test/listpair.sml
  2.    PS 1995-02-25, 1997-03-07
  3. *)
  4.  
  5. use "auxil.sml";
  6.  
  7. local 
  8.     open ListPair
  9.     val a = [1, 2, 3, 4, 5, 6]
  10.     val b = [10, 40, 50, 50]
  11.     val ab = [(1, 10), (2, 40), (3, 50), (4, 50)]
  12.     fun take 0 xs        = []
  13.       | take n []        = []
  14.       | take n (x :: xr) = x :: take (n-1) xr
  15. in 
  16.  
  17. val test1 = check(zip([], []) = [] 
  18.           andalso zip ([], a) = [] 
  19.           andalso zip(a, []) = []
  20.           andalso zip(a, b) = ab
  21.           andalso zip(b, a) = List.map (fn (x,y) => (y,x)) ab);
  22.  
  23. val test2a = check(([], []) = unzip []
  24.            andalso (a, a) = unzip(zip(a,a))
  25.            andalso (take (length b) a, b) = unzip(zip(a, b))
  26.            andalso (b, take (length b) a) = unzip(zip(b, a)));
  27. val test2b = check(ab = zip(unzip ab));
  28.  
  29. val test3a = check(map (fn (x, y) => x + y) (a, b) = 
  30.           List.map (fn (x,y) => x + y) (zip(a, b)));
  31.  
  32. local 
  33.     val v = ref 0
  34.     fun h [] r = r | h (x::xr) r = h xr (r+r+x): int;
  35.     val isum = h (take (length b) a) 0
  36. in 
  37.     fun reset () = v := 0;
  38.     fun incrv i = v := 2 * !v + i;
  39.     fun checkv () = check(!v = isum);
  40. end;
  41.  
  42. val test3b = (reset (); map (incrv o #1) (a, b) seq (); checkv());
  43.  
  44. val test4 = (reset (); app (incrv o #1) (a, b); checkv());
  45.  
  46. val test5a = check(all (fn _ => false) (a, [])
  47.            andalso not (exists (fn _ => true) ([], b))); 
  48.  
  49. val test5b = check(exists (fn (x, y) => x = 3) (a, b) 
  50.            andalso all (fn (x, y) => y <= 50) (a, b));
  51.  
  52. val test5c = check(not (exists (fn (x, y) => x = 5) (a, b))
  53.            andalso not (exists (fn (x, y) => y = 5) (b, a))
  54.            andalso all (fn (x, y) => x <> 6) (a, b)
  55.            andalso all (fn (x, y) => y <> 6) (b, a));
  56.  
  57. val test5d = (reset(); all (fn (x,y) => (incrv x; true)) (a, b) seq (); 
  58.           checkv());
  59. val test5e = (reset(); exists (fn (x,y) => (incrv x; false)) (a, b) seq (); 
  60.           checkv());
  61.  
  62. local 
  63.     fun foldrchk f e xs ys = 
  64.     foldr f e (xs, ys) = 
  65.     List.foldr (fn ((x, y), r) => f(x, y, r)) e (zip(xs, ys))
  66.     fun foldlchk f e xs ys = 
  67.     foldl f e (xs, ys) = 
  68.     List.foldl (fn ((x, y), r) => f(x, y, r)) e (zip(xs, ys))
  69. in
  70. val test6 = check'(fn _ => 
  71.     foldrchk (fn (x, y, (r1, r2)) => (x-r1, y div r2)) (0, 10) a b
  72.     andalso foldrchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] b
  73.     andalso foldrchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) a  []
  74.     andalso foldrchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] []);
  75.  
  76. val test7 = check'(fn _ => 
  77.     foldlchk (fn (x, y, (r1, r2)) => (x-r1, y div r2)) (0, 10) a b
  78.     andalso foldlchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] b
  79.     andalso foldlchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) a  []
  80.     andalso foldlchk (fn (x, y, (r1, r2)) => (x div r1, y div r2)) (0, 0) [] []);
  81. end
  82.  
  83. end;
  84.  
  85.